package com.zty.prince.tuangou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.zty.prince.tuangou.R;
import com.zty.prince.tuangou.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

public class GuideUIActivity extends AppCompatActivity {

    private ViewPager mVp;
    private Button mBtn_enter;
    private List<ImageView> mPics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_ui);

        /*viewpager来展示几个页面*/
        initview();
        initdata();
    }

    private void initview() {
        mVp = (ViewPager) findViewById(R.id.vp_guide);
        mBtn_enter = (Button) findViewById(R.id.btn_entermain);
    }
    /**
     * 初始化数据
     *
     * */
    private void initdata() {
        mPics = new ArrayList<>();
        int[] imgids = new int[]{R.mipmap.guide_1,R.mipmap.guide_2,R.mipmap.guide_3,R.mipmap.guide_4};
        for (int i = 0; i < imgids.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imgids[i]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mPics.add(imageView);
        }

        mVp.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mPics.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = mPics.get(position);
                container.addView(imageView);
                return  imageView;
            }
        });

        mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==mPics.size()-1){
                    mBtn_enter.setVisibility(View.VISIBLE);
                }else {
                    mBtn_enter.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mBtn_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtil.putFirstBoolean(GuideUIActivity.this,SplashActivity.IF_FITST_START,false);
                startActivity(new Intent(GuideUIActivity.this,MainActivity.class));
                finish();
                overridePendingTransition(android.support.v7.appcompat.R.anim.abc_popup_enter,
                        android.support.v7.appcompat.R.anim.abc_popup_exit);
            }
        });
    }
}
